package com.itheima.app.controller;

import com.itheima.app.manager.MovementManager;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    private MovementManager movementManager;
    //动态-发布  Content-Type = multipart/form-data 文件上传表单 不是json 不需要加@RequestBody
    @PostMapping("/movements")
    public ResponseEntity saveMovement(Movement movement , MultipartFile[] imageContent) throws IOException {


        return movementManager.saveMovement(movement  ,imageContent );
    }


    //查询我的个人动态
    @GetMapping("/movements/all")
    public ResponseEntity findMyMovement(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            Long userId
    ){
        return movementManager.findMyMovement(page , pagesize ,userId);
    }


    //查询好友动态
    @GetMapping("/movements")
    public ResponseEntity findFriendMovement(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize
    ){
        return movementManager.findFriendMovement(page , pagesize );
    }


    //查询推荐动态
    @GetMapping("/movements/recommend")
    public ResponseEntity findRecommendMovement(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize
    ){
        return movementManager.findRecommendMovement(page , pagesize );
    }


    //点赞
    @GetMapping("/movements/{publishId}/like")
    public ResponseEntity movementLike(
            @PathVariable String publishId
    ){
        return movementManager.movementLike(publishId);
    }
    //取消点赞
    @GetMapping("/movements/{publishId}/dislike")
    public ResponseEntity movementDislike(
            @PathVariable String publishId
    ){
        return movementManager.movementDislike(publishId);
    }


    //单条动态
    @GetMapping("/movements/{publishId}")
    public ResponseEntity findMovementsById(
            @PathVariable String publishId
    ){
        return movementManager.findMovementsById(publishId);
    }

    //评论列表
    @GetMapping("/comments")
    public ResponseEntity findCommentsList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            String movementId
    ){
        return movementManager.findCommentsList(page ,pagesize ,  movementId);
    }


    //保存评论
    @PostMapping("/comments")
    public ResponseEntity saveComment(
            @RequestBody Map<String,String> map
    ){
        String publishId = map.get("movementId");//动态id
        String comment = map.get("comment");//评论内容
        return movementManager.saveComment(publishId  ,comment);
    }
}
